package edu.bw.controller;

import edu.bw.po.Product;
import edu.bw.service.ProductService;
import edu.bw.vo.OperationStatus;
import edu.bw.vo.ResponseData;
import edu.bw.vo.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/selectAll")
    public ResponseData<List<Product>> selectAll(){
        try {
            List<Product> products = productService.findAll();
            return ResultUtil.result(OperationStatus.SUCCESS,products);
        } catch (Exception e) {
            return ResultUtil.result(OperationStatus.ERROR,null);
        }
    }
    @PostMapping("/updateProduct")
    public ResponseData<Product> updateProduct(@RequestBody Product product){
        try {
            productService.updateProduct(product);
            return ResultUtil.result(OperationStatus.SUCCESS,null);
        } catch (Exception e) {
            return ResultUtil.result(OperationStatus.ERROR,null);
        }
    }
    @PostMapping("/disProduct")
    public ResponseData<Product> disProduct(@RequestBody Product product){
        return ResultUtil.result(OperationStatus.SUCCESS,product);
    }
}
